package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.jms.Queue;
import javax.jms.Topic;
import java.util.HashMap;

@Api(value = "欢迎使用swagger")
@RestController
@CrossOrigin
@Slf4j
public class HelloController {

    @Resource
    private JmsTemplate jmsTemplate;
    @Resource
    private Queue queue;
    @Resource
    private Topic topic;


    @ApiOperation(value = "欢迎",notes = "欢迎-具体")
    @GetMapping("/hello")
    public String hello(){
        return "hello world";
    }

    @PostMapping("/testBody")
    public HashMap<String,Object> testBody(@RequestBody JSONObject user){
        log.info("user:{}",user);
        User u = JSONObject.toJavaObject(user,User.class);
        System.out.println(u);

        return null;
    }

    @RequestMapping("/queue")
    public String queue(){
        for (int i = 0; i < 10; i++){
            jmsTemplate.convertAndSend(queue,"queue"+i);
        }
        return "queue 发送成功";
    }

    @JmsListener(destination = "out.queue")
    public void consumerMsg(String msg){
        System.out.println(msg);
    }

    @RequestMapping("topic")
    public String topic(){
        for (int i = 0; i < 10; i++){
            jmsTemplate.convertAndSend(topic,"topic"+i);
        }
        return "topic 发送成功";
    }


}
